package com.demo.mq.rabbitmq.controller;

import java.util.UUID;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.mq.rabbitmq.callback.RabbitMQCallback;
import com.demo.mq.rabbitmq.config.RabbitMQConfig;

/**
 * @author lee9213@163.com
 * @version 1.0
 * @date 2017-04-04 15:29
 */
@RestController
public class SendMessageController{

    private RabbitTemplate rabbitTemplate;

    public SendMessageController(RabbitTemplate rabbitTemplate){
        this.rabbitTemplate = rabbitTemplate;
        this.rabbitTemplate.setConfirmCallback(new RabbitMQCallback());
    }

    /**
     * 向消息队列1发送消息
     *
     * @return
     */
    @RequestMapping("send1")
    public String send1() {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE, RabbitMQConfig.ROUTINGKEY1, "哈哈哈，消息1", correlationData);
        return null;
    }

    /**
     * 向消息队列2发送消息
     *
     * @return
     */
    @RequestMapping("send2")
    public String send2() {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE, RabbitMQConfig.ROUTINGKEY2, "哈哈哈，消息2", correlationData);
        return null;
    }

}
